package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.woniu_bx.exception.HelperCateNameIsExistException;
import com.woniu.woniu_bx.exception.HelperQuesNameIsExistException;
import com.woniu.woniu_bx.mapper.HelperQuesMapper;
import com.woniu.woniu_bx.pojo.HelperCate;
import com.woniu.woniu_bx.pojo.HelperQues;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.service.HelperQuesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sjy
 * @create 2022-08-27 17:31
 */
@Service
@Transactional
public class HelperQuesServiceImpl extends ServiceImpl<HelperQuesMapper, HelperQues> implements HelperQuesService {
    @Autowired
    private HelperQuesMapper helperQuesMapper;

    /**
     * 增加
     *
     * @param helperQues
     */
    @Override
    public void add(HelperQues helperQues) {
        if (helperQuesMapper.selectNameNum(helperQues.getName()) == 1) {
            throw new HelperQuesNameIsExistException("帮助中心问答题目已存在");
        }
        helperQuesMapper.insert(helperQues);
    }

    /**
     * 修改
     *
     * @param helperQues
     */
    @Override
    public void update(HelperQues helperQues) {
        int id = helperQues.getId();
        HelperQues oldHelperQues = helperQuesMapper.selectById(id);
        if (oldHelperQues.getName().equals(helperQues.getName())){
            helperQuesMapper.updateById(helperQues);
            return;
        }
        if (helperQuesMapper.selectNameNum(helperQues.getName()) == 1) {
            throw new HelperQuesNameIsExistException("帮助中心问答题目已存在");
        }
        helperQuesMapper.updateById(helperQues);
    }

    @Override
    public List<HelperQues> getByCategoryId(int categoryId) {
        return helperQuesMapper.selectByCategoryId(categoryId);
    }

    @Override
    public HelperQues getByID(int id) {
        return helperQuesMapper.getQuesByID(id);
    }

    @Override
    public PageBean<HelperQues> getQuesByPage(PageBean pageBean) {
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSize());
        List<HelperQues> result = helperQuesMapper.getAllQues();
        pageBean.setData(result);
        pageBean.setTotalPages(p.getPages());
        pageBean.setCurrPageSize(result.size());
        pageBean.setTotalNums((int)p.getTotal());
        return pageBean;
    }

    @Override
    public List<HelperQues> getAllQuesName() {
        List<HelperQues> helperQuesList = helperQuesMapper.getAllQuesName();
        return helperQuesList;
    }

    @Override
    public List<HelperQues> getQuesnameByCid(Integer cid) {
        List<HelperQues> quesnameByCid = helperQuesMapper.getQuesnameByCid(cid);
        return quesnameByCid;
    }

}
